package com.lagou;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.session.SqlSession;
import com.lagou.session.SqlSessionFactory;
import com.lagou.session.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

public class test {

    public static void main(String[] args) throws PropertyVetoException, DocumentException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        long start = System.currentTimeMillis();
        SqlSession sqlSession = factory.openSession();
//        User user1 = new User();
//        user1.setId(1);
//        user1.setUsername("lucy");
//        User user = sqlSession.selectOne("userMapper_selectOne", user1);
//        System.out.println(user.getBirthday());
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//
//        List<Object> list = sqlSession.selectList("userMapper_selectAll", null);
//        System.out.println(list.size());
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        List<User> list = dao.selectAll();
        System.out.println(list.toString());
    }

    @Test
    public void update() throws Exception {

        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = factory.openSession();
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(2);
        user.setUsername("mike");
        user.setPassword("456");
        user.setBirthday("2021-11-06");
        int update = dao.update(user);
        System.out.println(update);
    }

    @Test
    public void insert() throws Exception {

        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = factory.openSession();
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setUsername("nike");
        user.setPassword("777");
        user.setBirthday("2020-12-20");
        int insert = dao.insert(user);
        System.out.println(insert);
    }

    @Test
    public void delete() throws Exception {

        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = factory.openSession();
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        int delete = dao.delete(5);
        System.out.println(delete);
    }
}
